home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / command / build_py.py < prev    next >
Encoding:
Python Source  |  2000-08-15  |  12.8 KB  |  348 lines

  1. """distutils.command.build_py
  2.  
  3. Implements the Distutils 'build_py' command."""
  4.  
  5. # created 1999/03/08, Greg Ward
  6.  
  7. __revision__ = "$Id: build_py.py,v 1.27 2000/08/15 13:01:25 gward Exp $"
  8.  
  9. import sys, string, os
  10. from types import *
  11. from glob import glob
  12.  
  13. from distutils.core import Command
  14. from distutils.errors import *
  15.  
  16.  
  17. class build_py (Command):
  18.  
  19.     description = "\"build\" pure Python modules (copy to build directory)"
  20.  
  21.     user_options = [
  22.         ('build-lib=', 'd', "directory to \"build\" (copy) to"),
  23.         ('force', 'f', "forcibly build everything (ignore file timestamps"),
  24.         ]
  25.  
  26.  
  27.     def initialize_options (self):
  28.         self.build_lib = None
  29.         self.py_modules = None
  30.         self.package = None
  31.         self.package_dir = None
  32.         self.force = None
  33.  
  34.     def finalize_options (self):
  35.         self.set_undefined_options ('build',
  36.                                     ('build_lib', 'build_lib'),
  37.                                     ('force', 'force'))
  38.  
  39.         # Get the distribution options that are aliases for build_py
  40.         # options -- list of packages and list of modules.
  41.         self.packages = self.distribution.packages
  42.         self.py_modules = self.distribution.py_modules
  43.         self.package_dir = self.distribution.package_dir
  44.  
  45.  
  46.     def run (self):
  47.  
  48.         # XXX copy_file by default preserves atime and mtime.  IMHO this is
  49.         # the right thing to do, but perhaps it should be an option -- in
  50.         # particular, a site administrator might want installed files to
  51.         # reflect the time of installation rather than the last
  52.         # modification time before the installed release.
  53.  
  54.         # XXX copy_file by default preserves mode, which appears to be the
  55.         # wrong thing to do: if a file is read-only in the working
  56.         # directory, we want it to be installed read/write so that the next
  57.         # installation of the same module distribution can overwrite it
  58.         # without problems.  (This might be a Unix-specific issue.)  Thus
  59.         # we turn off 'preserve_mode' when copying to the build directory,
  60.         # since the build directory is supposed to be exactly what the
  61.         # installation will look like (ie. we preserve mode when
  62.         # installing).
  63.  
  64.         # Two options control which modules will be installed: 'packages'
  65.         # and 'py_modules'.  The former lets us work with whole packages, not
  66.         # specifying individual modules at all; the latter is for
  67.         # specifying modules one-at-a-time.  Currently they are mutually
  68.         # exclusive: you can define one or the other (or neither), but not
  69.         # both.  It remains to be seen how limiting this is.
  70.  
  71.         # Dispose of the two "unusual" cases first: no pure Python modules
  72.         # at all (no problem, just return silently), and over-specified
  73.         # 'packages' and 'py_modules' options.
  74.  
  75.         if not self.py_modules and not self.packages:
  76.             return
  77.         if self.py_modules and self.packages:
  78.             raise DistutilsOptionError, \
  79.                   "build_py: supplying both 'packages' and 'py_modules' " + \
  80.                   "options is not allowed"
  81.  
  82.         # Now we're down to two cases: 'py_modules' only and 'packages' only.
  83.         if self.py_modules:
  84.             self.build_modules ()
  85.         else:
  86.             self.build_packages ()
  87.  
  88.     # run ()
  89.         
  90.  
  91.     def get_package_dir (self, package):
  92.         """Return the directory, relative to the top of the source
  93.            distribution, where package 'package' should be found
  94.            (at least according to the 'package_dir' option, if any)."""
  95.  
  96.         path = string.split (package, '.')
  97.  
  98.         if not self.package_dir:
  99.             if path:
  100.                 return apply (os.path.join, path)
  101.             else:
  102.                 return ''
  103.         else:
  104.             tail = []
  105.             while path:
  106.                 try:
  107.                     pdir = self.package_dir[string.join (path, '.')]
  108.                 except KeyError:
  109.                     tail.insert (0, path[-1])
  110.                     del path[-1]
  111.                 else:
  112.                     tail.insert (0, pdir)
  113.                     return apply (os.path.join, tail)
  114.             else:
  115.                 # Oops, got all the way through 'path' without finding a
  116.                 # match in package_dir.  If package_dir defines a directory
  117.                 # for the root (nameless) package, then fallback on it;
  118.                 # otherwise, we might as well have not consulted
  119.                 # package_dir at all, as we just use the directory implied
  120.                 # by 'tail' (which should be the same as the original value
  121.                 # of 'path' at this point).
  122.                 pdir = self.package_dir.get('')
  123.                 if pdir is not None:
  124.                     tail.insert(0, pdir)
  125.  
  126.                 if tail:
  127.                     return apply (os.path.join, tail)
  128.                 else:
  129.                     return ''
  130.  
  131.     # get_package_dir ()
  132.  
  133.  
  134.     def check_package (self, package, package_dir):
  135.  
  136.         # Empty dir name means current directory, which we can probably
  137.         # assume exists.  Also, os.path.exists and isdir don't know about
  138.         # my "empty string means current dir" convention, so we have to
  139.         # circumvent them.
  140.         if package_dir != "":
  141.             if not os.path.exists (package_dir):
  142.                 raise DistutilsFileError, \
  143.                       "package directory '%s' does not exist" % package_dir
  144.             if not os.path.isdir (package_dir):
  145.                 raise DistutilsFileError, \
  146.                       ("supposed package directory '%s' exists, " +
  147.                        "but is not a directory") % package_dir
  148.  
  149.         # Require __init__.py for all but the "root package"
  150.         if package:
  151.             init_py = os.path.join (package_dir, "__init__.py")
  152.             if os.path.isfile (init_py):
  153.                 return init_py
  154.             else:
  155.                 self.warn (("package init file '%s' not found " +
  156.                             "(or not a regular file)") % init_py)
  157.  
  158.         # Either not in a package at all (__init__.py not expected), or
  159.         # __init__.py doesn't exist -- so don't return the filename.
  160.         return
  161.                 
  162.     # check_package ()
  163.  
  164.  
  165.     def check_module (self, module, module_file):
  166.         if not os.path.isfile (module_file):
  167.             self.warn ("file %s (for module %s) not found" % 
  168.                        (module_file, module))
  169.             return 0
  170.         else:
  171.             return 1
  172.  
  173.     # check_module ()
  174.  
  175.  
  176.     def find_package_modules (self, package, package_dir):
  177.         self.check_package (package, package_dir)
  178.         module_files = glob (os.path.join (package_dir, "*.py"))
  179.         modules = []
  180.         setup_script = os.path.abspath (sys.argv[0])
  181.  
  182.         for f in module_files:
  183.             abs_f = os.path.abspath (f)
  184.             if abs_f != setup_script:
  185.                 module = os.path.splitext (os.path.basename (f))[0]
  186.                 modules.append ((package, module, f))
  187.         return modules
  188.  
  189.  
  190.     def find_modules (self):
  191.         """Finds individually-specified Python modules, ie. those listed by
  192.         module name in 'self.py_modules'.  Returns a list of tuples (package,
  193.         module_base, filename): 'package' is a tuple of the path through
  194.         package-space to the module; 'module_base' is the bare (no
  195.         packages, no dots) module name, and 'filename' is the path to the
  196.         ".py" file (relative to the distribution root) that implements the
  197.         module.
  198.         """
  199.  
  200.         # Map package names to tuples of useful info about the package:
  201.         #    (package_dir, checked)
  202.         # package_dir - the directory where we'll find source files for
  203.         #   this package
  204.         # checked - true if we have checked that the package directory
  205.         #   is valid (exists, contains __init__.py, ... ?)
  206.         packages = {}
  207.  
  208.         # List of (package, module, filename) tuples to return
  209.         modules = []
  210.  
  211.         # We treat modules-in-packages almost the same as toplevel modules,
  212.         # just the "package" for a toplevel is empty (either an empty
  213.         # string or empty list, depending on context).  Differences:
  214.         #   - don't check for __init__.py in directory for empty package
  215.  
  216.         for module in self.py_modules:
  217.             path = string.split (module, '.')
  218.             package = string.join(path[0:-1], '.')
  219.             module_base = path[-1]
  220.  
  221.             try:
  222.                 (package_dir, checked) = packages[package]
  223.             except KeyError:
  224.                 package_dir = self.get_package_dir (package)
  225.                 checked = 0
  226.  
  227.             if not checked:
  228.                 init_py = self.check_package (package, package_dir)
  229.                 packages[package] = (package_dir, 1)
  230.                 if init_py:
  231.                     modules.append((package, "__init__", init_py))
  232.  
  233.             # XXX perhaps we should also check for just .pyc files
  234.             # (so greedy closed-source bastards can distribute Python
  235.             # modules too)
  236.             module_file = os.path.join (package_dir, module_base + ".py")
  237.             if not self.check_module (module, module_file):
  238.                 continue
  239.  
  240.             modules.append ((package, module_base, module_file))
  241.  
  242.         return modules
  243.  
  244.     # find_modules ()
  245.  
  246.  
  247.     def find_all_modules (self):
  248.         """Compute the list of all modules that will be built, whether
  249.         they are specified one-module-at-a-time ('self.py_modules') or
  250.         by whole packages ('self.packages').  Return a list of tuples
  251.         (package, module, module_file), just like 'find_modules()' and
  252.         'find_package_modules()' do."""
  253.  
  254.         if self.py_modules:
  255.             modules = self.find_modules ()
  256.         else:
  257.             modules = []
  258.             for package in self.packages:
  259.                 package_dir = self.get_package_dir (package)
  260.                 m = self.find_package_modules (package, package_dir)
  261.                 modules.extend (m)
  262.  
  263.         return modules
  264.  
  265.     # find_all_modules ()
  266.  
  267.  
  268.     def get_source_files (self):
  269.  
  270.         modules = self.find_all_modules ()
  271.         filenames = []
  272.         for module in modules:
  273.             filenames.append (module[-1])
  274.  
  275.         return filenames
  276.  
  277.  
  278.     def get_module_outfile (self, build_dir, package, module):
  279.         outfile_path = [build_dir] + list(package) + [module + ".py"]
  280.         return apply (os.path.join, outfile_path)
  281.  
  282.  
  283.     def get_outputs (self):
  284.         modules = self.find_all_modules ()
  285.         outputs = []
  286.         for (package, module, module_file) in modules:
  287.             package = string.split (package, '.')
  288.             outputs.append (self.get_module_outfile (self.build_lib,
  289.                                                      package, module))
  290.         return outputs
  291.  
  292.  
  293.     def build_module (self, module, module_file, package):
  294.         if type (package) is StringType:
  295.             package = string.split (package, '.')
  296.         elif type (package) not in (ListType, TupleType):
  297.             raise TypeError, \
  298.                   "'package' must be a string (dot-separated), list, or tuple"
  299.  
  300.         # Now put the module source file into the "build" area -- this is
  301.         # easy, we just copy it somewhere under self.build_lib (the build
  302.         # directory for Python source).
  303.         outfile = self.get_module_outfile (self.build_lib, package, module)
  304.         dir = os.path.dirname (outfile)
  305.         self.mkpath (dir)
  306.         self.copy_file (module_file, outfile, preserve_mode=0)
  307.  
  308.  
  309.     def build_modules (self):
  310.  
  311.         modules = self.find_modules()
  312.         for (package, module, module_file) in modules:
  313.  
  314.             # Now "build" the module -- ie. copy the source file to
  315.             # self.build_lib (the build directory for Python source).
  316.             # (Actually, it gets copied to the directory for this package
  317.             # under self.build_lib.)
  318.             self.build_module (module, module_file, package)
  319.  
  320.     # build_modules ()
  321.  
  322.  
  323.     def build_packages (self):
  324.  
  325.         for package in self.packages:
  326.  
  327.             # Get list of (package, module, module_file) tuples based on
  328.             # scanning the package directory.  'package' is only included
  329.             # in the tuple so that 'find_modules()' and
  330.             # 'find_package_tuples()' have a consistent interface; it's
  331.             # ignored here (apart from a sanity check).  Also, 'module' is
  332.             # the *unqualified* module name (ie. no dots, no package -- we
  333.             # already know its package!), and 'module_file' is the path to
  334.             # the .py file, relative to the current directory
  335.             # (ie. including 'package_dir').
  336.             package_dir = self.get_package_dir (package)
  337.             modules = self.find_package_modules (package, package_dir)
  338.  
  339.             # Now loop over the modules we found, "building" each one (just
  340.             # copy it to self.build_lib).
  341.             for (package_, module, module_file) in modules:
  342.                 assert package == package_
  343.                 self.build_module (module, module_file, package)
  344.  
  345.     # build_packages ()
  346.                        
  347. # class build_py
  348.